package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.utils.YMTDateUtil;

public class UserDeviceAuthWapper {
	SqlManager manage;

	public UserDeviceAuthWapper() {
		manage = new SqlManager("UserSafe");
	}

	/**
	 * 设备认证信息
	 * 
	 * @return Map
	 */
	public Map<String, Object> getDeviceAuthByUserIdAndDeviceId(int userId, String deviceId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("deviceId", deviceId);
		List<Map<String, Object>> deviceAuth = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.selectDeviceAuthByUserIdAndDeviceId", params);
		manage.commit();
		if(deviceAuth!=null && deviceAuth.size()>0){
			return deviceAuth.get(0);
		}
		return null;
	}

	/**
	 * IP认证信息
	 * 
	 * @return Map
	 */
	public Map<String, Object> getIPAuthByUserIdAndArea(int userId, String country, String province, String city) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		List<Map<String, Object>> ipAuth = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.selectIPAuthByUserIdAndDeviceId", params);
		manage.commit();
		if(ipAuth!=null && ipAuth.size()>0){
			return ipAuth.get(0);
		}
		return null;
	}
	/**
	 * IP认证信息
	 * 
	 * @return Map
	 */
	public List<Map<String, Object>> getIPAuthByUserIdAndAreas(int userId, String country, String province, String city) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		List<Map<String, Object>> ipAuth = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.selectIPAuthByUserIdAndDeviceId", params);
		manage.commit();
		if(ipAuth!=null && ipAuth.size()>0){
			return ipAuth;
		}
		return null;
	}
	/**
	 * 获取相应状态的的设备认证信息
	 * @param authStatus 认证状态(200表示认证成功)
	 * @param authTime(Token认证时间)
	 * @return
	 */
	public Map<String, Object> getDeviceAuth(int authStatus, String authTime, String deviceId){
		List<Map<String, Object>> deviceAuth = getDeviceAuthData(authStatus, authTime, deviceId);
		manage.commit();
		if(deviceAuth!=null && deviceAuth.size()>0){
			return deviceAuth.get(0);
		}
		return null;
	} 
	
	/**
	 * 获取认证成功的设备信息
	 * @param authStatus
	 * @param authTime
	 * @return
	 */
	public List<Map<String, Object>> getDeviceAuthData(Integer authStatus, String authTime, String deviceId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("authStatus", authStatus);
		params.put("authTime", authTime);
		params.put("deviceId", deviceId);
		List<Map<String, Object>> deviceAuth = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.getDeviceAuth", params);
		manage.commit();
		return deviceAuth;
	}
	
	/**
	 * 获取相应状态的的IP区域认证信息
	 * @param authStatus 认证状态(200表示认证成功)
	 * @param authTime(Token认证时间)
	 * @return
	 */
	public Map<String, Object> getIPAuth(int authStatus, String authTime, String country, String province){
		List<Map<String, Object>> ipAuth = getIPAuthData(authStatus, authTime, country, province);
		manage.commit();
		if(ipAuth!=null && ipAuth.size()>0){
			return ipAuth.get(0);
		}
		return null;
	} 
	
	/**
	 * 获取认证成功的IP区域信息
	 * @param authStatus
	 * @param authTime
	 * @return
	 */
	public List<Map<String, Object>> getIPAuthData(int authStatus, String authTime, String country, String province){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("authStatus", authStatus);
		params.put("authTime", authTime);
		params.put("country", country);
		params.put("province", province);
		List<Map<String, Object>> ipAuth = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.getIpAuth", params);
		manage.commit();
		return ipAuth;
	} 
	
	/**
	 * 删除认证过的IP区域记录，便于测试
	 * @param userId
	 * @param country
	 * @param province
	 * @param city
	 */
	public void deleteIPAreaData(int userId, String country, String province, String city){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		manage.delete("com.ymttest.database.mapping.UserDeviceAuthMapper.deleteByIPAreaAndUserId", params);
		manage.commit();
	}
	
	/**
	 * 删除设备信息
	 */
	public void deleteDeviceData(int userId, String deviceId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("deviceId", deviceId);
		manage.delete("com.ymttest.database.mapping.UserDeviceAuthMapper.deleteDeviceData", params);
		manage.commit();
	}
	
	/**
	 * 修改IP区域token最后认证时间
	 * @throws Exception 
	 */
	public int updateTokenUpdateTime(Integer userId, String country, String province, String city, String lastAuthTime) throws Exception{
		Map<String, Object> params = new HashMap<String, Object>();
		if(userId==null||userId==0){
			throw new Exception("userid错误");
		}
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		params.put("lastAuthTime", lastAuthTime);
		int i=manage.update("com.ymttest.database.mapping.UserDeviceAuthMapper.updateTokenUpdateTime", params);
		manage.commit();
		return i;
	}
	
	/**
	 * 修改区域Token最后认证时间
	 * @throws Exception 
	 */
	public void updateTokenUpdateTime(Integer userId, String deviceId, String lastAuthTime) throws Exception{
		Map<String, Object> params = new HashMap<String, Object>();
		if(userId==null||userId==0){
			throw new Exception("userid错误");
		}
		params.put("userId", userId);
		params.put("deviceId", deviceId);
		params.put("lastAuthTime", lastAuthTime);
		manage.update("com.ymttest.database.mapping.UserDeviceAuthMapper.updateDeviceTokenUpdateTime", params);
		manage.commit();
	}
	
	/**
	 * 插入设备认证记录
	 */
	public void insertDataToUserDeviceAuth(int userId, String deviceId, int authStatus, String authTime, String createTime,
			String appSource, String lastUpdateTime, int type, String description){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("deviceId", deviceId);
		params.put("authStatus", authStatus);
		params.put("authTime", authTime);
		params.put("createTime", createTime);
		params.put("appSource", appSource);
		params.put("lastUpdateTime", lastUpdateTime);
		params.put("type", type);
		params.put("description", description);
		manage.insert("com.ymttest.database.mapping.UserDeviceAuthMapper.insertDataToUserDeviceAuth", params);
		manage.commit();
	}
	
	/**
	 * 修改认证状态
	 * @param userId 用户Id
	 * @param deviceId 设备Id
	 * @param authStatus 认证状态
	 */
	public void updateAuthStatus(int userId, String deviceId, int authStatus){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("deviceId", deviceId);
		params.put("authStatus", authStatus);
		manage.update("com.ymttest.database.mapping.UserDeviceAuthMapper.updateDeviceAuthStatus", params);
		manage.commit();
	}
	
	/**
	 * 修改认证状态
	 * @param userId 用户Id
	 * @param deviceId 设备Id
	 * @param authStatus 认证状态
	 */
	public void updateAuthStatus(int userId, String country, String province, String city, int authStatus){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		params.put("authStatus", authStatus);
		manage.update("com.ymttest.database.mapping.UserDeviceAuthMapper.updateIpAuthStatus", params);
		manage.commit();
	}
	
	/**
	 * 插入IP认证记录
	 */
	public void insertUserIpAuth(int userId, String country, String province, String city, int authStatus,
			 String authTime, String createTime, String appSource, String lastLoginTime, String lastUpdateTime){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		params.put("authStatus", authStatus);
		params.put("authTime", authTime);
		params.put("createTime", createTime);
		params.put("appSource", appSource);
		params.put("lastUpdateTime", lastUpdateTime);
		params.put("lastUpdateTime", lastUpdateTime);
		manage.insert("com.ymttest.database.mapping.UserDeviceAuthMapper.insertUserIpAuth", params);
		manage.commit();
	}
	
	/**
	 * 加入设备Token认证时间数据
	 */
	public void insertDeviceTokenAuthHistory(int userId, String devicerId, int type, String lastAuthTime, String creatTime){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("devicerId", devicerId);
		params.put("type", type);
		params.put("lastAuthTime", lastAuthTime);
		params.put("creatTime", creatTime);
		manage.insert("com.ymttest.database.mapping.UserDeviceAuthMapper.insertDeviceTokenAuthHistory", params);
		manage.commit();
	}
	
	/**
	 * 插入IP Token认证时间数据
	 */
	public void insertIPTokenAuthHistory(int userId, String country, String province, String city, String lastAuthTime, String createTime){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("country", country);
		params.put("province", province);
		params.put("city", city);
		params.put("lastAuthTime", lastAuthTime);
		params.put("createTime", createTime);
		manage.insert("com.ymttest.database.mapping.UserDeviceAuthMapper.insertIPTokenAuthHistory", params);
		manage.commit();
	}
	
	/**
	 * 获取config信息
	 * @param appId
	 * @param code
	 * @param key
	 */
	public Map<String, Object> getLoginSafeConfiguration(String appId, String code, String key){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appId", appId);
		params.put("code", code);
		params.put("key", key);
		List<Map<String, Object>> configs = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.getLoginSafeConfiguration", params);
		manage.commit();
		if(configs!=null && configs.size()>0){
			return configs.get(0);
		}
		return null;
	}
	/**
	 * 更新
	 * @param appId
	 * @param code
	 * @param key
	 * @param value
	 * @return
	 * @throws InterruptedException 
	 */
	public int updateNewLoginSafeConfiguration(String appId, String code, String key,String value) throws InterruptedException{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appId", appId);
		params.put("code", code);
		params.put("key", key);
		params.put("value", value);
		int i= manage.update(
				"com.ymttest.database.mapping.UserDeviceAuthMapper.updateNewLoginSafeConfiguration", params);
		manage.commit();
		return i;
	}
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
		manage = null;
	}

	public static void main(String[] args) throws InterruptedException {
//		Map<String, Object> userAuth = new UserDeviceAuthWapper().getDeviceAuthByUserIdAndDeviceId(123, "000");
//		Map<String, Object> userAuth = new UserDeviceAuthWapper().getDeviceAuth(200, DateUtils.getBeforeOrNextDay(-90),"aaaa");
//		System.out.print(userAuth.keySet());
//		System.out.print(userAuth.values());
//		new UserDeviceAuthWapper().updateTokenUpdateTime(4003, "New Zealand", "New Zealand", "*", DateUtils.getBeforeOrNextDay(-100));
//		Map<String, Object> userAuth = new UserDeviceAuthWapper().getIPAuthByUserIdAndDeviceId(3976, "China", "Shanghai", "Shanghai");
//		System.out.print(userAuth);
		String time = YMTDateUtil.getBeforeOrNextDay(0);
		UserDeviceAuthWapper wapper = new UserDeviceAuthWapper();
		wapper.updateNewLoginSafeConfiguration("Seller", "LoginPas", "Open", "1");
//		wapper.insertDataToUserDeviceAuth(92, "testinsert", 200, time, time, "app", time, 1,"test");
//		wapper.insertUserIpAuth(92, "test",  "test",  "test", 200, time, time, "mainsite", time, time);
//		wapper.insertIPTokenAuthHistory(92, "test",  "test",  "test", time, time);
//		wapper.insertDeviceTokenAuthHistory(92, "test", 1, time, time);
//		wapper.updateTokenUpdateTime(4370, "tokenLost", DateUtils.getBeforeOrNextDay(-100));
	}
}
